#include "guard.h"
#include "../board.h"
#include "../gameSettings.h"
#include "../soundManager.h"

// checks to see if there is something of interest in
// the line of sight
void Guard::look() {
	Board &board = *getCurrentBoard();

	int lookX = x;
	int lookY = y;

	int dx;
	int dy;
	getDxAndDy(o, dx, dy);

	// advance off of the the self-same tile
	lookX += dx;
	lookY += dy;

	while (board.inBounds(lookX, lookY)) {
		// can't look past wall
		if (board.getTile(lookX, lookY) == WALL) {
			break;
		}

		// get the object we're currently looking at
		Actor *obj;
		ObjectType objType = board.getObject(lookX, lookY, obj);

		if (objType == OB_ILL && board.getIll().isKillable())
		{
			playSfx(SFX_GUARD_DISCOVERS_PLAYER);
			obj->kill();
			board.setLose();
			break;
		}
		// if target is a guard and not self
		else if (objType == OB_GUARD && !(this->isAt(lookX, lookY)))
		{
			if (obj->isFakeIll()) {
				playSfx(SFX_GUARD_KILLS_GUARD);
				obj->kill();
			}

			// can't see past enemy
			break;
		}
		else if (objType == OB_DOG)
		{
			if (obj->isFakeIll()) {
				playSfx(SFX_GUARD_KILLS_GUARD);
				playSfx(SFX_DOG_DIES);
				obj->kill();
			}

			// can't see past enemy
			break;
		}
		else if (objType == OB_SG)
		{
			if (obj->isFakeIll()) {
				playSfx(SFX_GUARD_KILLS_GUARD);
				obj->kill();
			}

			// can't see past enemy
			break;
		}
		else if (objType == OB_FOOD)
		{
			// guards approach food
			if (!approachFlag) {	// not in approach mode
				approachFlag = true;
				addCallback(new TimedCallback(approachCallback, GUARD_WALK_PACE, false));
			}
		}
		else if (objType == OB_DEADBODY)
		{
			// guards are afraid of bodies
			if (!fleeFlag) {		// not in flee mode
				playSfx(SFX_GUARD_SEES_BODY);
				fleeFlag = true;
				addCallback(new TimedCallback(fleeCallback, GUARD_WALK_PACE, true));
			}
		}

		lookX += dx;
		lookY += dy;
	}

	// if we're out of plance and we are not doing anything else
	if (!atOrigLoc() && !inManipulateMode()) {
		// set callback to move back to original location
		if (!returnOrigLocFlag) {
			returnOrigLocFlag = true;
			addCallback(new TimedCallback(returnOrigLocCallback, GUARD_WALK_PACE, true));
		}
	}
}

// end of guard.cpp
